package com.stadium.servlet.impl;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;

import com.stadium.bean.Order;
import com.stadium.bean.Place;
import com.stadium.bean.PlaceOrder;
import com.stadium.repository.PlaceOrderRepository;
import com.stadium.repository.PlaceRepository;
import com.stadium.servlet.PlaceOrderServlet;
import com.stadium.servlet.PlaceService;

@Service(value = "PlaceOrderServlet")
public class PlaceOrderServletImpl implements PlaceOrderServlet {

	@Autowired
	private PlaceOrderRepository placeOrderRepository;

	@Autowired
	private PlaceRepository placeRepository;

	public List<PlaceOrder> findAll(Integer type) {
		if (type == null) {
			List<PlaceOrder> orders = placeOrderRepository.findAll();
			setPlace(orders);
			return orders;
		} else {
			List<PlaceOrder> orders = placeOrderRepository.findAll(type);
			setPlace(orders);
			return orders;
		}
	}

	public PlaceOrder addPlaceOrder(PlaceOrder placeOrder) throws ParseException {
		if (placeOrderRepository.judgeTime(placeOrder.getStartDate(), placeOrder.getEndDate()).size() == 0) {
			SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");// 设置日期格式
			String date = df.format(new Date());// new Date()为获取当前系统时间
			placeOrder.setOrderTime(date);
			Double hours=getHour(placeOrder.getStartDate(),placeOrder.getEndDate()); //获取相隔时间
			Double placeMoney=placeRepository.findOne(placeOrder.getPlaceId()).getPlaceMoney();//获取场地价格
			placeOrder.setAllMoney(placeMoney*hours);
			return(placeOrderRepository.saveAndFlush(placeOrder));
		} else {
			throw new SecurityException("订单时间冲突");
		}
	}

	public void setType(Integer placeId, Integer type) {
		PlaceOrder placeOrder = placeOrderRepository.findOne(placeId);
		if (placeOrder.getStatus() == 2) {
			throw new SecurityException("订单已经完成不能修改状态");
		} else {
			placeOrder.setStatus(type);
			placeOrderRepository.saveAndFlush(placeOrder);
		}
	}

	public void deletePlaceOrder(Integer placeId) {
		PlaceOrder placeOrder = placeOrderRepository.findOne(placeId);
		if (placeOrder.getStatus() != 0) {
			throw new SecurityException("订单已经完成审核不能删除");
		} else {
			placeOrderRepository.delete(placeId);
		}
	}

	/*
	 * 给订单加上场地信息
	 */
	void setPlace(List<PlaceOrder> orders) {
		for (PlaceOrder placeOrder : orders) {
			Place place = placeRepository.findOne(placeOrder.getPlaceId());
			placeOrder.setPlace(place);
		}
	}

	/*
	 * 两个时间戳获取相隔的小数数量
	 */
	public Double getHour(String startTime, String endTime) throws ParseException {
		Date d1 = null;
		Date d2 = null;

		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

		d1 = sdf.parse(startTime);
		d2 = sdf.parse(endTime);

		long dd1 = d1.getTime();
		long dd2 = d2.getTime();
		double hours = (double) (dd2 - dd1) / 3600 / 1000;
		return hours;
	}

	public List<PlaceOrder> findAllByBorrowAccount(String name) {
		return addPlace(placeOrderRepository.findByBorrowAccount(name));
	}

	List<PlaceOrder> addPlace(List<PlaceOrder> lists){
		for(PlaceOrder placeOrder:lists){
			Place place=placeRepository.findOne(placeOrder.getPlaceId());
			placeOrder.setPlace(place);
		}
		return lists;
	}

}
